// 由于已使用固定域名配置，简化环境变量加载
const envConfig = require("dotenv").config();

// 引入基础URL配置
const baseUrlConfig = require('./baseurl.js');

console.log(process.env.NODE_ENV, 'process.env.NODE_ENV');


export default {
  // 开发服务器配置
  server: {
    port: 8900, // 默认端口
    host: 'localhost' // 默认主机，如需局域网访问可设为 '0.0.0.0'
  },
  // SPA 模式配置
  ssr: false,
  router: {
    // 使用 'hash' 主要是为了适配以相对路径打开的静态站点， 必须使用 'hash' 否则路由跳转不生效
    mode: "hash",
    // 开发环境不使用 base，生产环境打包时使用相对路径
    base: "/h5/",

    //路由中间件
    middleware: ["i18n"],

    // scrollBehavior(to, from, savedPosition) {
    //   // 如果存在缓存位置（浏览器前进/后退）
    //   if (savedPosition) {
    //     return savedPosition;
    //   } else {
    //     // 默认滚动到顶部
    //     return { x: 0, y: 0 };
    //   }
    // },    
  },
  // Target: https://go.nuxtjs.dev/config-target
  target: "static",
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: "Youdianstar",
    htmlAttrs: {
      lang: "vi"
    },
    meta: [{
        charset: "utf-8"
      },
      {
        name: "viewport",
        content: "width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1, user-scalable=no"
      },
      {
        name: "format-detection",
        content: "telephone=yes"
      },
      {
        hid: "description",
        name: "description",
        content: ""
      }
    ],
    link: [{
      rel: "icon",
      type: "image/x-icon",
      href: "logo.png"
    }],
    script: [
      //Apple login
      {
        src: `https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js`,
        type: "text/javascript",
        async: true,
        defer: true
      },

      // Google login
      {
        src: "https://accounts.google.com/gsi/client",
        type: "text/javascript",
        async: true,
        defer: true
      },      
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [{
      src: "@/assets/css/common.scss"
    },
    {
      src: "@/assets/css/common.css"
    },
    {
      src: "@/assets/css/animation.scss"
    },
    {
      src: "element-ui/lib/theme-chalk/index.css"
    },
    {
      src: "node_modules/swiper/css/swiper.css"
    },
    {
      src: "vant/lib/index.less"
    }
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [{
      src: "@/plugins/i18n.js",
      ssr: true
    },
    {
      src: "@/plugins/moments",
      ssr: true
    },
    {
      src: "@/plugins/element-ui",
      ssr: false
    },
    {
      src: "@/plugins/vue-awesome-swiper",
      ssr: false
    },
    {
      src: "@/plugins/vant-ui",
      ssr: false
    },
    {
      src: "@/plugins/common",
      ssr: false
    },
    {
      src: "@/plugins/vue-qrcode-reader",
      ssr: false
    },
    {
      src: "@/plugins/qrcode",
      ssr: false
    },
    {
      src: "@/plugins/vconsole",
      ssr: false
    }
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    '@nuxtjs/proxy'
  ],

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: [/^element-ui/],
    vendor: ["vue-i18n"],
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {},
    optimization: {
      splitChunks: {
        minSize: 10000, //最小
        maxSize: 40000 //最大
      }
    }
  },
  // 环境配置
  env: {
    ...process.env,
    ...envConfig.parsed,
    // 直接使用baseurl.js中的配置
    BASE_URL: baseUrlConfig.BASE_URL,
    H5_URL: baseUrlConfig.H5_URL
  },

  // 代理配置
  proxy: {
    '/api': {
      target: 'https://api.youdianstar.vn',
      changeOrigin: true,
      pathRewrite: {
        '^/api': '/api'
      }
    }
  },

  // 静态站点打包配置: https://www.nuxtjs.cn/api/configuration-generate
  // 当运行 nuxt generate 命令或在编码中调用 nuxt.generate() 时，Nuxt.js 会使用 generate 属性的配置ss
  generate: {
    dir: "dist"
  }
};
